﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Rison.Web.Common;
using Rison.BusinessFacade.Helper;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Entity.XT;
using Rison.BusinessFacade.Facade.XT;
using Rison.DataAccess.Common;

namespace Rison.Web.admin
{
    public partial class SysUserView : PageDataViewBase
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            base.labMessage = this.labMsg;
            base.dataView = this.gvData;
            base.editPage = "SysUserEdit.aspx";
            base.addButton = this.btnAppend;
            base.deleteButton = this.btnDelete;            
            base.baseFacade = new SysUserFacade();            
        }

        protected override List<Condition> GetConditions()
        {
            List<Condition> conditions = base.GetConditions();
            if (conditions == null) conditions = new List<Condition>();

            if (!StringHelper.IsNullOrEmpty(this.txtUid.Text))
                conditions.Add(new Condition("Sys_Users.Userid", SqlOperator.Like, txtUid.Text.Trim()));
            if (!StringHelper.IsNullOrEmpty(this.txtUName.Text))
                conditions.Add(new Condition("Sys_Users.Name", SqlOperator.Like, txtUName.Text.Trim()));

            return conditions;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.labMsg.Text)) labMsg.Text = string.Empty;

            if (!this.IsPostBack)
            {
                BindData();
            }
        }


        protected override List<string> GetCannotDeleteKeyValueOrName()
        {
            List<string> values = new List<string>();
            values.Add("1"); values.Add("admin");
            return values;
        }

        protected void btnInitPwd_Click(object sender, EventArgs e)
        {
            List<int> selectedItems = base.GetSelectedItemsIndex();
            if (selectedItems == null || selectedItems.Count == 0)
            {
                this.labMsg.Text = "请勾选要初始化密码的数据行！";
                return;
            }

            foreach (int index in selectedItems)
            {
                SysUser user = baseFacade.Retrieve(Convert.ToInt64(this.gvData.DataKeys[index].Value)) as SysUser;
                if (user != null)
                {
                    user.Password = GlobalParams.Instance.UserDefaultPassword;

                    if (baseFacade.Update(user) < 0)
                    {
                        this.labMsg.Text = "初始化密码错误！";
                        return;
                    }
                }
            }

            this.labMsg.Text = "初始化密码完成！";
            this.BindData();
        }
    }
}
